<?php
namespace app\index\controller;

use app\index\controller\Base;
use app\common\model\Upload as UploadModel;

class Upload extends Base
{
    public function initialize()
    {
        parent::initialize();
        $this->Model = new UploadModel();
    }

    public function create()
    {
        $backcall = $this->request->param("backcall");
        $backcall_name = $this->request->param("backcall_name");
        $width = $this->request->param("width");
        $height = $this->request->param("height");
        $image = $this->request->param("image");
        $upload_type = $this->request->param("upload_type");

        if($this->request->isPost()){
            $file = $this->request->file('image');
            $upload_info = $this->Model->saveFile($file, 'cover');
            $image = $upload_info["save_path"];
        }
        $this->assign("backcall", $backcall);
        $this->assign("backcall_name", $backcall_name);
        $this->assign("width", $width);
        $this->assign("height", $height);
        $this->assign("image", $image);
        $this->assign("upload_type", $upload_type);
        return $this->fetch();
    }

    public function add()
    {
        $backcall = $this->request->param("backcall");
        $backcall_name = $this->request->param("backcall_name");
        $width = $this->request->param("width");
        $height = $this->request->param("height");
        $upload_type = $this->request->param("upload_type");
        $id = $this->request->param("id");
        if(intval($id) > 0){
            $upload = $this->Model->where('id', $id)->find();
        }

        if(!empty($upload)){
            $image = $upload["save_path"];
        }else{
            $image = "";
        }

        if($this->request->isPost()){
            $file = $this->request->file('image');
            $upload_info = $this->Model->saveFile($file, 'cover');
            $image = $upload_info["save_path"];
            $id = $upload_info["id"];
        }
        $this->assign("backcall", $backcall);
        $this->assign("backcall_name", $backcall_name);
        $this->assign("upload_type", $upload_type);
        $this->assign("width", $width);
        $this->assign("height", $height);
        $this->assign("image", $image);
        $this->assign("id", $id);
        return $this->fetch();
    }

    /**
     * umeditor编辑器上传方法
     * @Author   zhanghong(Laifuzi)
     * @DateTime 2018-06-13
     * @return   [type]             [description]
     */
    public function umeditor()
    {
        if($this->request->isPost()){
            $file = $this->request->file('upfile');
            $upload_info = $this->Model->saveFile($file, 'ueditor');
            $res = [
                "originalName" => $upload_info["file_name"],
                "name" => $upload_info["file_name"],
                "url" => $upload_info["save_path"],
                "size" => $upload_info["size"],
                "type" => ".".$upload_info["ext"],
                "state" => "SUCCESS"
            ];
            echo(json_encode($res));
        }else{
            echo('');
        }
    }

    /**
     * simditor编辑器上传方法
     * @Author   zhanghong(Laifuzi)
     * @DateTime 2018-06-13
     * @return   [type]             [description]
     */
    public function simditor()
    {
        if($this->request->isPost()){
            $file = $this->request->file('upfile');
            $upload_info = $this->Model->saveFile($file, 'ueditor');
            $res = [
                "origin_name" => $upload_info["origin_name"],
                "file_path" => $upload_info["save_path"],
                "success" => true
            ];
            return json($res);
        }else{
            echo('');
        }
    }
}
